﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.Mvc;
using System.Web.Routing;
using P.InOut.NetMvc.Models;
using P.InOut.NetMvc.Properties;

namespace P.InOut.NetMvc
{
    public class GlobalApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            StarterKits.Mvc.Membership.Controllers.BaseFormsAuthenticationAdministrationController.RegisterDefaultRoutes(routes, "Security", "FormsAuthenticationAdministration", null); //change null to ".mvc" for IIS6
            StarterKits.Mvc.Membership.Controllers.BaseFormsAuthenticationController.RegisterDefaultFormsRoutes(routes, "FormsAuthentication", null); //change null to ".mvc" for IIS6
            StarterKits.Mvc.Membership.Controllers.BaseFormsAuthenticationController.RegisterDefaultOpenIDRoutes(routes, "FormsAuthentication", null); //change null to ".mvc" for IIS6

            routes.MapRoute("Default",
                                "{controller}/{action}/{id}",
                                new { controller = "Home", action = "Index", id = "" },
                                new { controller = @"[^\.]*" });
            routes.MapRoute("Homepage",
                                "Default.aspx",
                                new { controller = "Home", action = "Index", id = "" });
        }

        /// <summary>
        /// 
        /// </summary>
        public static void RegisterMembership()
        {
            using (InOutDataContext db = new InOutDataContext(Settings.Default.dbConn))
            {
                if (db.Teams.Count() == 0)
                {
                    Team team = new Team()
                    {
                        Description = "Default Team"
                    };

                    db.Teams.InsertOnSubmit(team);
                    db.SubmitChanges();
                }

                List<string> roles = Roles.GetAllRoles().ToList<string>();
                if (roles.Count == 0)
                {
                    Roles.CreateRole("Administrator");
                    Roles.CreateRole("User");
                }

                MembershipUserCollection users = Membership.GetAllUsers();
                if (users.Count == 0)
                {
                    Membership.CreateUser("administrator", "password", "admin@inout.com");
                    Roles.AddUserToRole("administrator", "Administrator");

                    MembershipUser user = Membership.GetUser("administrator");

                    Profile profile = new Profile
                    {
                        UserId              = (Guid)user.ProviderUserKey,
                        DateTimeLastUpdated = DateTime.Now,
                        InStatus            = false,
                        OfficeStatus        = false,
                        Status              = "I was just created!"
                    };

                    db.Profiles.InsertOnSubmit(profile);

                    Team team = db.Teams.Where(t => t.Description == "Default Team").Single();
                    MemberTeam memberTeam = new MemberTeam
                    {
                        UserId              = (Guid)user.ProviderUserKey,
                        TeamId              = team.Id
                    };

                    db.MemberTeams.InsertOnSubmit(memberTeam);

                    db.SubmitChanges();
                }
            }
        }

        protected void Application_Start()
        {
            RegisterRoutes(RouteTable.Routes);
            RegisterMembership();
        }
    }
}